package com.kbxwn.logmanager;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.kbxwn.logmanager.entity.EventMessage;
import com.kbxwn.logmanager.entity.HistoryTagAdapter;
import com.kbxwn.logmanager.entity.LogEntity;
import com.kbxwn.logmanager.log.LimitQueue;
import com.kbxwn.logmanager.log.LogManager;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

public class FilterActivity extends Activity {
    @BindView(R.id.textView3)
    TextView textView;

    @BindView(R.id.scrollView)
    public ScrollView scrollView;

    @BindView(R.id.panel_condition)
    public View panelConditionView;

    @BindView(R.id.condition_listView)
    public ListView historyTagListView;

    @BindView(R.id.editText)
    public EditText edtCondtition;

    private HistoryTagAdapter historyTagAdapter;

    private Handler handler = new Handler();

    private String filterLogTag = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter_log);
        ButterKnife.bind(this);

        initHistoryTag();

        EventBus.getDefault().register(this);
    }

    private void initHistoryTag() {
        historyTagAdapter = new HistoryTagAdapter(this);
        historyTagListView.setAdapter(historyTagAdapter);
        historyTagAdapter.setOnClickListener(new HistoryTagAdapter.OnClickItemListener() {
            @Override
            public void onItemClick(String tag) {
                edtCondtition.setText(tag);
            }
        });
    }

    private void initLog() {
        textView.setText("");

        LimitQueue<LogEntity> logQueue = LogManager.intance().getLogQueue();
        for (LogEntity logEntity : logQueue) {
            if (logEntity.checkTag(filterLogTag)) {
                textView.append(Html.fromHtml(logEntity.log));
                textView.append("\n");
            }
        }

        scrollToBottom();
    }

    @OnClick(R.id.btn_back)
    public void clickBack() {
        finish();
    }

    @OnClick(R.id.panel_condition)
    public void clickPanel() {
        hideKeyboard();
        panelConditionView.setVisibility(View.GONE);
    }

    @OnClick(R.id.btn_filter)
    public void clickFilter() {
        hideKeyboard();
        panelConditionView.setVisibility(View.GONE);

        filterLogTag = edtCondtition.getText().toString();

        historyTagAdapter.addTag(filterLogTag);

        if (filterLogTag.length() == 0) {
        } else {
            initLog();
        }
    }

    @OnClick(R.id.btn_filter_condition)
    public void clickCondition() {
        panelConditionView.setVisibility(View.VISIBLE);
        historyTagListView.requestFocus();
        historyTagListView.requestFocusFromTouch();
    }

    @OnClick(R.id.btn_clear)
    public void clickClean() {
        textView.setText("");
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void logFresh(EventMessage msg) {
        if (msg.checkTag("show_new_log")) {
            LogEntity logEntity = (LogEntity) msg.object;
            if (logEntity.checkTag(filterLogTag)) {
                textView.append(Html.fromHtml(logEntity.log));
                textView.append("\n");
                scrollToBottom();
            }
        }
    }

    private void scrollToBottom() {
        handler.post(new Runnable() {
            @Override
            public void run() {
                scrollView.smoothScrollTo(0, 100000);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        historyTagAdapter.onSave();
        EventBus.getDefault().unregister(this);
    }

    protected void hideKeyboard() {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (getCurrentFocus() != null) {
            inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 2);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (panelConditionView.getVisibility() == View.VISIBLE) {
                hideKeyboard();
                panelConditionView.setVisibility(View.GONE);
                return true;
            } else {
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
